package com.fll.fangyishare.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fll.fangyicommon.entity.ResultOk;
import com.fll.fangyicommon.entity.share.Comment;
import com.fll.fangyicommon.entity.share.CommentLiked;
import com.fll.fangyicommon.entity.share.Liked;
import com.fll.fangyicommon.entity.user.User;
import com.fll.fangyishare.service.CommentLikedService;
import com.fll.fangyishare.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

/**
 * @author fll
 * create 2023/3/26
 */
@RestController
@Slf4j
@RequestMapping("/share")
public class CommentLikedController {

    @Autowired
    private CommentLikedService commentLikedService;

    //登录用户点赞某个share的评论
    @GetMapping("/addCommentLiked")
    public ResultOk addCommentLiked(@RequestParam Map<String, String> params, @RequestHeader String userInfo) {
        ResultOk<CommentLiked> ok = new ResultOk<>();
        Integer shareId = Integer.parseInt(params.get("shareId"));
        Integer commentId = Integer.parseInt(params.get("commentId"));
        if (shareId == null){
            ok.setCode(400);
            ok.setMsg("shareId不能为空");
            return ok;
        }
        if (commentId == null){
            ok.setCode(400);
            ok.setMsg("commentId不能为空");
            return ok;
        }

        User user = null;
        try {
            user = new ObjectMapper().readValue(userInfo, User.class);
            Boolean islLike = commentLikedService.isLike(user.getUserId(), shareId, commentId);
            if (islLike){
                ok.setCode(400);
                ok.setMsg("已经点过赞啦");
                return ok;
            }
            //没有点过赞则添加记录
            CommentLiked commentLiked = new CommentLiked();
            commentLiked.setUserId(user.getUserId());
            commentLiked.setShareId(shareId);
            commentLiked.setCommentId(commentId);
            Integer num = commentLikedService.addCommentLiked(commentLiked);
            if (num != -1){
                ok.setCode(200);
                ok.setMsg("点赞成功");
                return ok;
            }
            ok.setCode(400);
            ok.setMsg("点赞失败");
            return ok;
        } catch (Exception e) {
            e.printStackTrace();
            ok.setCode(503);
            ok.setMsg("后端异常");
            return ok;
        }
    }

    //登录用户取消某个share点赞
    @GetMapping("/cancelCommentLiked")
    public ResultOk cancelCommentLiked(@RequestParam Map<String, String> params, @RequestHeader String userInfo) {
        ResultOk ok = new ResultOk<>();
        Integer shareId = Integer.parseInt(params.get("shareId"));
        Integer commentId = Integer.parseInt(params.get("commentId"));
        if (shareId == null){
            ok.setCode(400);
            ok.setMsg("shareId不能为空");
            return ok;
        }
        if (commentId == null){
            ok.setCode(400);
            ok.setMsg("commentId不能为空");
            return ok;
        }
        User user = null;
        try {
            user = new ObjectMapper().readValue(userInfo, User.class);
            CommentLiked commentLiked = new CommentLiked();
            commentLiked.setUserId(user.getUserId());
            commentLiked.setShareId(shareId);
            commentLiked.setCommentId(commentId);
            Integer num = commentLikedService.cancelCommentLiked(commentLiked);
            if(num != -1){
                ok.setMsg("取消成功");
                ok.setCode(200);
            }else {
                ok.setCode(400);
                ok.setMsg("取消失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            ok.setCode(503);
            ok.setMsg("后端异常");
        }
        return ok;
    }

}
